<?php
//ham dieu khien
	function a_display(){
		global $task;
		switch($task){
			case 'edit':
				edit();
				break;
			case 'delete':
				delete();
				break;
			case 'save':
			case 'savex':
				save();
				break;
			case 'add':
				viewAdd();
				break;
			case 'cancel':
				cancel();
				break;	
			default:
				viewDefault();
				break;
		}
	}
	
	
	function viewDefault(){
		require_once('base/class.pagination.php');
		$search = Request::get('search');
		$typeId = Request::get('q_type');
		$lessonId = Request::get('lesson_id');
		$lms = Request::get('limitstart',0);
		$total = getCountRows($search);
		$pageNav = new Pagination($total,$lms,100);
		$rows = getAllRows($search,$typeId,$lessonId);
		?>
		<table class="toolbar-fitter" border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
			<tr>
				<td width="100%"><input type="search" name="search" value="<?php echo $search;?>" /><input style="border-radius:8px;margin-left:5px;background:#ccc;" class="search" type="submit" value="Search" /></td>
				<td nowrap="nowrap">
					<?php  getTypeList(); //getTestTypeList(); ?>
					<?php  getLessonList(); ?>
					<input class="next" type="submit" name="search-fitter" value="Xem" />
				</td>
			</tr>
		</table>
		
		<table class="adminlist">
			<thead>
            	<tr>
               		<th width="10">STT</th>
                    <th width="10" ><input type="checkbox" value="on" name="allbox" onclick="checkAll();"/></th>
                    <th nowrap="nowrap">Question</th>
                    <th nowrap="nowrap">answer 1</th>
                    <th nowrap="nowrap">answer 2</th>
                    <th nowrap="nowrap">answer 3</th>
                    <th nowrap="nowrap">Answer4</th>
                    <th nowrap="nowrap">Correct</th>
                    <th nowrap="nowrap">Test Type</th>
                    <th nowrap="nowrap">Lesson</th>
                    <th nowrap="nowrap" width="1">ID</th>
               	</tr>
            </thead>
            <tbody>
            <?php 
			$i = 1; foreach($rows AS $row) {
			?>
            	<tr>
                	<td><?php echo $pageNav->getOfset($i);?></td>
                    <td><input id="actions-box" name="id[]" value="<?php echo $row->q_id; ?>"  type="checkbox"/></td>
                    <td><a href=""><?php echo $row->q_question;?></a></td>
                    <td><?php echo $row->q_answer1;?></td>
                    <td><?php echo $row->q_answer2;?></td>
                    <td><?php echo $row->q_answer3;?></td>
                    <td><?php echo $row->q_answer4;?></td>
                    <td><?php echo 'answer '.$row->q_correct;?></td>
                    <td><?php echo getTestType($row->q_type); ?></td>
                    <td><?php echo getLessons($row->q_lesson_id); ?></td>
                    <td nowrap="nowrap" style="color:gray;"><?php echo $row->q_id;?></td>
              	</tr>
             <?php $i++; } ?>
	            <tr>
					<td style="border:none !important;" colspan="12"><?php $pageNav->displayCpanel();?></td>
				</tr>
            </tbody>
        </table>
		<?php
		echo '<input type="hidden" name="option" value="question" />';
	}
	
	function viewAdd(&$record=null){
	?>
	<div class="t">
 		<div class="t">
			<div class="t"></div>
 		</div>
	</div>
	<div class="m">
	<table border="0" cellpadding="0" cellspacing="0" width="100%">
	<tbody>
	<tr>
		<td valign="top">
			<table class="adminform">
			<tbody>
			
			<tr>
				<td><p style="text-indent:20px;font-size:18px;font-family:Times New Roman, Times, serif;">Test Type</p></td>
				<td><?php if ($record) getTypeList($record->q_type); else getTypeList(); ?></td>
			</tr>
			
			<tr>
				<td><p style="text-indent:20px;font-size:18px;font-family:Times New Roman, Times, serif;">Lesson</p></td>
				<td><?php if ($record) getLessonList($record->q_lesson_id); else getLessonList(); ?></td>
			</tr>
			
			<tr>
				<td><p style="text-indent:20px;font-size:18px;font-family:Times New Roman, Times, serif;">Question</p></td>
				<td><input style="height:30px;" value="<?php if($record) echo $record->q_question;?>" type="search" size="60" name="q_question" /><br></td>
			</tr>
			
			<tr>
				<td><p style="text-indent:20px;font-size:18px;font-family:Times New Roman, Times, serif;">answer 1</p></td>
				<td><input style="height:30px;" value="<?php if($record) echo $record->q_answer1;?>" type="search" size="60" name="q_answer1" /><br></td>
			</tr>
			
			<tr>
				<td><p style="text-indent:20px;font-size:18px;font-family:Times New Roman, Times, serif;">answer 2</p></td>
				<td><input style="height:30px;" value="<?php if($record) echo $record->q_answer2;?>" type="search" size="60" name="q_answer2" /><br></td>
			</tr>
			
			<tr>
				<td><p style="text-indent:20px;font-size:18px;font-family:Times New Roman, Times, serif;">answer 3</p></td>
				<td><input style="height:30px;" value="<?php if($record) echo $record->q_answer3;?>" type="search" size="60" name="q_answer3" /><br></td>
			</tr>
			
			<tr>
				<td><p style="text-indent:20px;font-size:18px;font-family:Times New Roman, Times, serif;">answer 4</p></td>
				<td><input style="height:30px;" value="<?php if($record) echo $record->q_answer4;?>" type="search" size="60" name="q_answer4" /><br></td>
			</tr>
			
			<tr>
				<td><p style="text-indent:20px;font-size:18px;font-family:Times New Roman, Times, serif;">Correct</p></td>
				<td>
				    <select name="q_correct" style="width:330px;height:30px;">
				    <?php 
				    for ($i = 1; $i <= 4; $i++){
				    	if ($record && $record->q_correct == $i){
				    		echo '<option selected="selected" value="'.$i.'">answer '.$i.'</option>';
				    	}else {
				    	    echo '<option value="'.$i.'">answer '.$i.'</option>';	
				    	}
				    ?>   	
				    <?php }?>
				    </select>
				<br></td>
			</tr>
			</tbody></table>
	</td></tr></tbody></table>
	<div class="clr"></div>
	</div><!-- end m -->
	<div class="b">
		<div class="b">
			<div class="b"></div>
		</div>
	</div>
	<div class="clr"></div>
	<input type="hidden" name="option" value="question" />
	<input type="hidden" name="q_id" value="<?php if($record) echo $record->q_id;?>" />
	<div class="clr"></div>
	<!--<?php
	}
	
	
//Cac function process
	function save(){
		global $dbo;
		/*$id = 1;
		for ($i = 1; $i <= 7; $i++) {
			for ($j = 7; $j <= 10; $j++) {
				for ($j2 = 1; $j2 <= 10; $j2++) {
					$correct = rand(1, 4);
					$query = "INSERT INTO `question`(`q_id`, `q_question`, `q_answer1`, `q_answer2`, `q_answer3`, `q_answer4`, `q_correct`, `q_type`, `q_lesson_id`, `q_dataversion_id`) 
					VALUES (".$id.",'Question ".$j2."','Answer 1','Answer 2','Answer 3','Answer 4',".$correct.",".$i.",".$j.",1)";
			        $dbo->setQuery($query);
		            $dbo->query();
		            $id += 1;
				}
			}
		}
		*/
		$excute = includeTable();
		$excute->bind();
	    global $task;
		$question = trim(Request::get('q_question'));
		$answer1 = trim(Request::get('q_answer1'));
		$answer2 = trim(Request::get('q_answer2'));
		$answer3 = trim(Request::get('q_answer3'));
		$answer4 = trim(Request::get('q_answer4'));
		$type = Request::get('q_type');
		$correct = trim(Request::get("q_correct"));
		$lessonId = trim(Request::get('lesson_id'));
		$excute->q_lesson_id = $lessonId;
		
	  if (!$question || !$answer1 || !$answer2 || !$answer3 || !$answer2 || !$type ){
			    Message::setMessage('Please enter full',1);
		    }else {
		    //lay ve phien ban data moi nhat trong bang dataversionid -> de cap nhat g_dataversionid
		    	$dataVersionCurrent = getDataversionIdMax();
		    	$excute->q_dataversion_id = $dataVersionCurrent +1;
	            if(!$excute->store()){
		    	    Message::setMessage('False',1);
		        }else{
		    	     Message::setMessage('Saved',0);
		             //set session 
    	             if (!isset($_SESSION['updatedata'])){
    		             $_SESSION['updatedata']=true;
    	             }
		        }
		 }
		switch($task){
			case 'save':
				redirect('index.php?option=question');
				break;
			case 'savex':
				redirect('index.php?option=question&task=add');
				break;
		}
		
	}
	
	function setError($msg){
		$errors[] = $msg;
		return $errors;
	}
	
	
	function edit(){
		$excute = includeTable();
		$id = Request::get('id');
		$excute->load($id[0]);
		viewAdd($excute);	
	}
	
	function cancel(){
		$option = Request::get('option');
		redirect('index.php?option='.$option);
	}
	
	
	function delete(){
		$id = Request::get('id');
		//insert 1 record bang vao bang datadelete
	for ($i = 0; $i < count($id); $i++) {
			insertDataDeleted('question', $id[$i]);
		}
		$excute = includeTable();
		$excute->delete($id);
		//redirect('index.php?option=vocabulary');
		viewDefault();	
	}
	
	
	
/************************************************************************************/
	/**
	 * 
	 * Hien thi test type
	 * @param $lessionId
	 */
	function getTestType($tid){
		global $dbo;
		$dbo->setQuery("SELECT t_type FROM testtype WHERE t_id = '$tid' ");
		$row = $dbo->loadObjectList();
		foreach ($row AS $r)
			return $r->t_type;
	}
	
	
	/**
	 * 
	 * Get list of the lesson to search
	 */
/*	function getTestTypeList() {
		global $dbo;
		$dbo->setQuery("SELECT t_id,t_type FROM testtype ORDER BY t_id ASC ");
		$rows = $dbo->loadObjectList();
			echo '<select name="q_type">';
				echo '<option value="">--Select Type--</option>';
			foreach ($rows as $row){?>
				<option value="<?php echo $row->t_id; ?>"><?php echo $row->t_type; ?></option>
			<?php }
			echo '</select>';
	}*/
	
	
	/**
	 * 
	 * Hien thi list type khi add/ edit/ search
	 * @param $lid
	 */
	function getTypeList($id='') {
		global $dbo;
		$dbo->setQuery("SELECT t_id,t_type FROM testtype ORDER BY t_id ASC ");
		$rows = $dbo->loadObjectList();
			echo '<select style="width:330px;height:30px;" name="q_type">';
			echo '<option value="">--Select Type--</option>';
			foreach ($rows as $row){?>-->
				<option <?php if($id && $row->t_id == $id) echo 'selected="selected"'?> value="<?php echo $row->t_id; ?>"><?php echo $row->t_type; ?></option>
			<?php }
			echo '</select>';
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param $searchText
	 * @param $lessonId
	 */
	function getCountRows($searchText='',$testtype='',$lessonId=''){
		
		global $dbo;
		$query = "SELECT COUNT(q_id) FROM question ";
		
	    $where = array();
		if ($searchText){
			$where[] .=  " (`q_question` LIKE '%$searchText%' 
			            OR `q_answer1` LIKE '%$searchText%' 
			            OR `q_answer2` LIKE '%$searchText%' 
			            OR `q_answer3` LIKE '%$searchText%' 
			            OR `q_answer4` LIKE '%$searchText%' 
			            )"; 
		}
		
	    if ($testtype){
			$where[] .=  " `q_type` = ".$testtype;
		}
		
	    if ($lessonId){
			$where[] .=  " `q_lesson_id` = ".$lessonId;
		}
		
		
	    if (count($where) == 1){
			$query .= " WHERE ".$where[0];
		}
		
		if (count($where) > 1){
			$w = implode(' AND ', $where);
			$query .= " WHERE ".$w;
		}
		$dbo->setQuery($query);
		return $dbo->loadResult();
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param $searchText
	 * @param $lessonId
	 */
	function getAllRows($searchText='',$testtype='',$lessonId=''){
		global $dbo; 
		$lm = Request::get('limit',100);
		$lms = Request::get('limitstart',0);
		
	    $query = "SELECT q_id,q_question,q_answer1,q_answer2,q_answer3,q_answer4,q_type,q_lesson_id,q_correct
	              FROM question ";
		
	    $where = array();
	    if ($searchText){
			$where[] .=  " (`q_question` LIKE '%$searchText%' 
			            OR `q_answer1` LIKE '%$searchText%' 
			            OR `q_answer2` LIKE '%$searchText%' 
			            OR `q_answer3` LIKE '%$searchText%' 
			            OR `q_answer4` LIKE '%$searchText%' 
			            )"; 
		}
	    if ($testtype){
			$where[] .=  " `q_type` = ".$testtype;
		}
		
	    if ($lessonId){
			$where[] .=  " `q_lesson_id` = ".$lessonId;
		}
		
		
	    if (count($where) == 1){
			$query .= " WHERE ".$where[0];
		}
		if (count($where) > 1){
			$w = implode(' AND ', $where);
			$query .= " WHERE ".$w;
		}
		$query .= " ORDER BY q_id ASC LIMIT $lms,$lm ";
	    $dbo->setQuery($query);
		return $dbo->loadObjectList();
	}
	
	
	function getLessons($lessionId){
		global $dbo;
		$dbo->setQuery("SELECT le_title FROM lesson WHERE le_id = '$lessionId' ");
		$row = $dbo->loadObjectList();
		foreach ($row AS $r)
			return $r->le_title;
	}
	
	function getLessonList($id='') {
		global $dbo;
		$dbo->setQuery("SELECT le_id,le_title FROM lesson ORDER BY le_id ASC ");
		$rows = $dbo->loadObjectList();
			echo '<select style="width:330px;height:30px;" name="lesson_id">';
			echo '<option value="">--Select Type--</option>';
			foreach ($rows as $row){?>-->
				<option <?php if($id && $row->le_id == $id) echo 'selected="selected"'?> value="<?php echo $row->le_id; ?>"><?php echo $row->le_title; ?></option>
			<?php }
			echo '</select>';
	}
?>